package lyj.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lyj.springcloud.entities.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流控规则
 */
@RestController
@RequestMapping("/flowrule")
public class FlowRuleController {

    /**
     * 流控规则-----直接
     *
     * @return
     */
    @GetMapping("/direct")
    public CommonResult direct() {
        return new CommonResult("流控规则-----直接");
    }


    /**
     * 流控规则-----关联
     *
     * @return
     */
    @GetMapping("/associationa")
    public CommonResult associationA() {
        return new CommonResult("流控规则-----关联-----A 资源");
    }


    /**
     * 流控规则-----关联
     *
     * @return
     */
    @GetMapping("/associationb")
    public CommonResult associationB() {
        return new CommonResult("流控规则-----关联 B 资源");
    }


    /**
     * 流控规则-----·链路
     *
     * @return
     */
    @GetMapping("/link")
    public CommonResult link() {
        return new CommonResult("流控规则-----链路");
    }


    @GetMapping("/SentinelResource/test")
    @SentinelResource(value = "byresource", blockHandler = "flowcontroll")
    public CommonResult test() {

        return new CommonResult<String>();
    }

    /**
     * 服务降级兜底方法：返回值必须与业务方法返回值一致；方法必须带有参数：BlockException；
     *
     * @param e
     * @return
     */
    public CommonResult flowcontroll(BlockException e) {
        return new CommonResult("服务异常");
    }
}
